package com.rms.hkpc.util;

import java.util.Random;

public class CaptchaUtil {
    private static volatile CaptchaUtil instance;

    public static CaptchaUtil getInstance() {
        if (instance == null) {
            synchronized (CaptchaUtil.class) {
                if (instance == null) {
                    instance = new CaptchaUtil();
                }
            }
        }
        return instance;
    }

    public CaptchaUtil() {
    }

    /**
     * 生成数字及字母组合的验证码
     *
     * @param lenght 验证码长度
     * @return
     */
    public String getRandomCode(int lenght) {
        StringBuilder val = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < lenght; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            if ("char".equalsIgnoreCase(charOrNum)) {
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val.append((char) (random.nextInt(26) + temp));
            } else {
                val.append(random.nextInt(10));
            }
        }
        return val.toString();
    }
}
